package app.netant.book.base;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import cn.lily.com.db.DBFactory;
import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.enviroment.Enviroment;
import cn.lily.com.lang.number.MathUtil;
import app.netant.NetAntBase;

public abstract class BasePage extends NetAntBase {

	public BasePage(String netSite) {
		super(netSite);
	}
	
	public BasePage(String netSite,int timeout) {
		super(netSite,timeout);
	}
	
	protected String tableKey;
	protected String selectKey;
	protected String preHref;
	protected String baseType;
	
	/**
	 * 信息解析
	 */
	public void analyse(){
		
		Elements activePart = document.select(this.selectKey);
		if(activePart.size()>0){
			for(int i=0;i<activePart.size();i++){	
				
				Enviroment.getEnv().getLogBean().beginLog();
				
				String href = this.buildHref(activePart.get(i));
				
				if(href == null || "".equals(href)){
					continue;
				}
				
				System.out.println(href + "地址开始处理.........");
						
				BaseDetail detail = null;
				try {
					
					DataTable dataTable = this.getDBManager().executeQueryTable("select * from ${schema}.NET_"+this.tableKey.trim().toUpperCase()+"_INFO where NOVEL_URL='"+href+"'");
					if(dataTable.next()){
						continue;
					}

					BaseEntity entity = this.buildEntity();
					entity.setNovelUrl(href);
					
					if(this.baseType != null && !"".equals(this.baseType)){
						entity.setNovelType(baseType);
					}
					
					this.afterEntity(entity,activePart.get(i));
					
					detail = this.buildDetail(href);		
					detail.setEntity(entity);
					
					detail.analyse();
					
				} catch (Exception e) {
					
					e.printStackTrace();
					
					DBFactory.getThreadDB().executeUpdate("insert into ${schema}.NET_"+this.tableKey.trim().toUpperCase()+"_EXCEP (EXCEP_ID,EXCEP_URL,EXCEP_LEVEL) "
														+ "values ('"+System.currentTimeMillis()+""+MathUtil.random(3)+"','"+href+"','2')");
					
					continue;
					
				} finally {
					if(detail != null){
						detail.free();
					}
				}
				
				
				Enviroment.getEnv().getLogBean().endLog();

			}
		}
			
	}
	
	public void analyseHref(){	
		Elements activePart = document.select(this.selectKey);
		if(activePart.size()>0){
			for(int i=0;i<activePart.size();i++){	
				
				Enviroment.getEnv().getLogBean().beginLog();
				
				String href = this.buildHref(activePart.get(i));
				
				if(href == null || "".equals(href)){
					continue;
				}
				
				System.out.println(href + "地址开始处理.........");
				Enviroment.getEnv().getLogBean().endLog();

			}
		}
	}
	
	public void splitTags(String tagAlls,BaseEntity entity){
		
		String[] tagm = tagAlls.split("：");
		String keys = "";
		String vals = "";
		
		for(int i=0;i<tagm.length;i++){
			
			String tmp = tagm[i].trim();
			
			if(i==0){
				keys += "," + tmp.replaceAll(" ", "");
			}
			else if(i==tagm.length-1){
				vals += "," + tmp.replaceAll(" ", "");
			}
			else{
				
				int first = tmp.indexOf(" ");
				if(first>0){
					keys += "," + tmp.substring(first).replaceAll(" ", "");
					vals += "," + tmp.substring(0,first).replaceAll(" ", "");
				}
				
			}
			
		}
		
		if(!"".equals(keys)){
			keys = keys.substring(1);
			vals = vals.substring(1);
		}
		
		String[] keyS = keys.split(",");
		String[] valS = vals.split(",");
		
		for(int j=0;j<keyS.length;j++){
			
			System.out.println("【Tag】"+keyS[j]+"="+valS[j]);
		
			entity.setTag(keyS[j], valS[j]);
				
		}	
	}
	
	public void setTableKey(String tableKey) {
		this.tableKey = tableKey;
	}

	public void setSelectKey(String selectKey) {
		this.selectKey = selectKey;
	}
	
	public String getBaseType() {
		return baseType;
	}

	public void setBaseType(String baseType) {
		this.baseType = baseType;
	}

	public abstract String buildHref(Element element);
	
	public abstract BaseEntity buildEntity();
	
	public abstract BaseDetail buildDetail(String href);
	
	public abstract void afterEntity(BaseEntity entity, Element element);
}
